﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AnimalCare.Model;

namespace AnimalCare.Service
{
    /// <summary>
    ///
    /// </summary>
    class ProductService
    {
        /// <summary>
        /// Searches the specified name.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public static List<ProductDetail> search(string name)
        {
            List<ProductDetail> productList = new List<ProductDetail>();
            using (var ctx = new AnimalCareDBEntities())
            {
                var products = (from s in ctx.ProductDetails
                                where s.product_name.Contains(name)
                                select s);
                productList = products.ToList();
            }
            return productList;
        }

        public static List<ProductDetail> search(int classId)
        {
            List<ProductDetail> productList = new List<ProductDetail>();
            using (var ctx = new AnimalCareDBEntities())
            {
                var products = (from s in ctx.ProductDetails
                                where s.classification_id == classId
                                select s);
                productList = products.ToList();
            }
            return productList;
        }

        /// <summary>
        /// Inserts the specified p.
        /// </summary>
        /// <param name="p">The p.</param>
        public static void Insert(Product p)
        {
            using (var ctx = new AnimalCareDBEntities())
            {
                ctx.Products.AddObject(p);
                ctx.SaveChanges();
            }
        }
        /// <summary>
        /// Adds the specified p.
        /// </summary>
        /// <param name="p">The p.</param>
        public static void Add(Product p)
        {
            using (var ctx = new AnimalCareDBEntities())
            {
                int total = 0;
                var product = (from s in ctx.Products
                               where s.product_id == p.product_id
                               select s).FirstOrDefault();
                if (product.total == null)
                {
                    total = p.total.Value;
                }
                else
                    if (product.total.Value == 0)
                    {
                        total = p.total.Value;
                    }
                    else
                    {
                        total = p.total.Value + product.total.Value;
                    }
                copyProduct(ref product, p);
                product.total = total;
                int num = ctx.SaveChanges();
            }
        }


        /// <summary>
        /// Updates the specified p.
        /// </summary>
        /// <param name="p">The p.</param>
        public static void Update(Product p)
        {
            using (var ctx = new AnimalCareDBEntities())
            {
                var product = (from s in ctx.Products
                               where s.product_id == p.product_id
                               select s).FirstOrDefault();
                product = p;
                int num = ctx.SaveChanges();
            }
        }
        public static void UpdateTotal(Product p)
        {
            using (var ctx = new AnimalCareDBEntities())
            {
                var product = (from s in ctx.Products
                               where s.product_id == p.product_id
                               select s).FirstOrDefault();
                product.total = p.total;
                int num = ctx.SaveChanges();
            }
        }

        /// <summary>
        /// Deletes the specified identifier.
        /// </summary>
        /// <param name="id">The identifier.</param>
        public static void Delete(int id)
        {
            using (var ctx = new AnimalCareDBEntities())
            {
                var product = (from s in ctx.Products
                               where s.product_id == id
                               select s).FirstOrDefault();
                ctx.Products.DeleteObject(product);
                ctx.SaveChanges();
            }
        }
        /// <summary>
        /// Copies the product detail to product.
        /// </summary>
        /// <param name="p">The p.</param>
        /// <param name="pd">The pd.</param>
        public static void copyProduct(ref Product p, Product pd)
        {
            p.classification_id = pd.classification_id;
            p.discount = pd.discount;
            p.expiry_date = pd.expiry_date;
            p.infomation = pd.infomation;
            p.ingredient = pd.ingredient;
            p.packaging_id = pd.packaging_id;
            p.position_id = pd.position_id;
            p.price = pd.price;
            p.product_id = pd.product_id;
            p.product_name = pd.product_name;
            p.promotion = pd.promotion;
            p.supplier_id = pd.supplier_id;
            p.total = pd.total;
            p.type_id = pd.type_id;
            p.unit_price = pd.unit_price;
        }
        public static ProductDetail copyProductDetail(ProductDetail pd)
        {
            ProductDetail p = new ProductDetail();
            p.classification_id = pd.classification_id;
            p.classification_name = pd.classification_name;
            p.discount = pd.discount;
            p.expiry_date = pd.expiry_date;
            p.infomation = pd.infomation;
            p.ingredient = pd.ingredient;
            p.packaging_id = pd.packaging_id;
            p.Packaging_name = pd.Packaging_name;
            p.position_id = pd.position_id;
            p.position_name = pd.position_name;
            p.price = pd.price;
            p.product_id = pd.product_id;
            p.product_name = pd.product_name;
            p.promotion = pd.promotion;
            p.supplier_id = pd.supplier_id;
            p.supplier_name = pd.supplier_name;
            p.total = pd.total;
            p.type_id = pd.type_id;
            p.type_name = pd.type_name;
            p.unit_price = pd.unit_price;
            return p;
        }
        public static Product copyProductDetailToProduct(ProductDetail pd)
        {
            Product p = new Product();
            p.classification_id = pd.classification_id;
            p.discount = pd.discount;
            p.expiry_date = pd.expiry_date;
            p.infomation = pd.infomation;
            p.ingredient = pd.ingredient;
            p.packaging_id = pd.packaging_id;
            p.position_id = pd.position_id;
            p.price = pd.price;
            p.product_id = pd.product_id;
            p.product_name = pd.product_name;
            p.promotion = pd.promotion;
            p.supplier_id = pd.supplier_id;
            p.total = pd.total;
            p.type_id = pd.type_id;
            p.unit_price = pd.unit_price;
            return p;
        }
    }
}
